package com.piesat.quake.common.mqtt.decoder;

import com.piesat.quake.common.mqtt.decoder.entity.FdDataPc;
import com.piesat.quake.common.mqtt.decoder.common.CommonDecoder;
import com.piesat.quake.common.mqtt.decoder.entity.FdData;

import java.util.ArrayList;
import java.util.List;

public class FdDataDecoder extends CommonDecoder<FdData> {

    @Override
    public FdData decoder() {
        //读取基础信息
        FdData data = new FdData();
        data.setType(this.readString(2));
        data.setLength(this.readIntegerLE());
        data.setId(this.readString(8));
        data.setEndian(this.readByte());
        data.setTimestamp(Double.valueOf(this.readDoubleLE()*1000.0).longValue());
        data.setChPid(this.readByte());
        data.setKey(data.getId() + "_" + data.getTimestamp());
        List<FdDataPc> fdDataPcList = new ArrayList<>();
        //读取数据块
        byte flag = 1;
        while (flag > 0) {
            FdDataPc fdDataPc = new FdDataPc();
            fdDataPc.setNo(this.readByte());
            fdDataPc.setNextNo(this.readByte());
            flag = fdDataPc.getNextNo();
            fdDataPc.setLength(this.readIntegerLE());
            fdDataPc.setChId(this.readByte());
            fdDataPc.setMax1s(this.readFloatLE());
            fdDataPc.setMin1s(this.readFloatLE());
            fdDataPc.setMax5s(this.readFloatLE());
            fdDataPc.setMin5s(this.readFloatLE());
            fdDataPc.setMax30s(this.readFloatLE());
            fdDataPc.setMin30s(this.readFloatLE());
            fdDataPc.setAvg3s(this.readFloatLE());
            fdDataPc.setAvg60s(this.readFloatLE());
            fdDataPc.setLsAvgPct(this.readFloatLE());
            fdDataPc.setStatus(this.readByte());
            fdDataPcList.add(fdDataPc);
        }
        data.setPcList(fdDataPcList);
        return data;
    }

    public FdDataDecoder(byte[] bytes) {
        super(bytes);
    }
}
